package org.jeecg.modules.qyxx.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.Rest;
import org.jeecg.modules.qyxx.entity.CompanyInfo;
import org.jeecg.modules.qyxx.entity.req.KfptDoServiceRequest;
import org.jeecg.modules.qyxx.entity.req.NsrsbhShxydmRequst;
import org.jeecg.modules.qyxx.entity.resp.BaseResponse;
import org.jeecg.modules.qyxx.mapper.CompanyInfoMapper;
import org.jeecg.modules.qyxx.service.ILoginService;
import org.jeecg.modules.qyxx.service.IUserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.jeecg.modules.qyxx.util.BszsCommonUtils.transBaseResponseToRest;

@Service
@Slf4j
public class UserCenterServiceImpl implements IUserCenterService {

//    @Value("${foresee.usercenter.url}")
//    private String foreseeUsercenterUrl;
//
//    @Value("${foresee.usercenter.appid}")
//    private String foreseeUsercenterAppid;
//
//    @Value("${foresee.usercenter.signKey}")
//    private String foreseeUsercenterSignKey;

    @Autowired
    private CompanyInfoMapper companyInfoMapper;
   /* @Autowired
    private UserInfoMapper userInfoMapperMapper;*/

    @Autowired
    private ILoginService iLoginService;
    /*@Autowired
    private ILoginYhzhService iLoginYhzhService;
    @Autowired
    private SwyAPIService swyAPIService;
    @Autowired
    private NsrService nsrService;
    @Autowired
    private IUserCenterService userCenterService;*/

//    @Value("${foresee.gryhmmkey.key}")
    private String gryhmmkey; //个人用户密码加密key


    /*@Override
    public GetUnifiedTokenResponse getUnifiedToken(String nsrsbh, String areaCode, String swjgdm) {

        GetUnifiedTokenResponse getUnifiedTokenResponse = new GetUnifiedTokenResponse();
        Map<String, String> param = new HashMap<>();
        param.put("taxpayerId", nsrsbh); //纳税人识别号
        String url = foreseeUsercenterUrl + "/v1/usercenter/org/getUnifiedToken";
        OpenApiClient openApiClient = new OpenApiClient(foreseeUsercenterAppid, foreseeUsercenterSignKey);
        String responseJson = openApiClient.requestApp(url, JSON.toJSONString(param), "10001003");
        log.info("getUnifiedToken返回数据:{},taxpayerId:{}", responseJson, nsrsbh);
        log.info("1.2.6.凭企业获取统一会话凭证 返回数据{}", responseJson);
        if (responseJson != null) {
            getUnifiedTokenResponse = JSON.parseObject(responseJson, GetUnifiedTokenResponse.class);

        }
        if (getUnifiedTokenResponse != null && "0".equals(getUnifiedTokenResponse.getHead().getErrorCode())) {
            RedisUtil.set(RedisKeyConstant.redisKfptSessionKey + nsrsbh, "UC_" + getUnifiedTokenResponse.getBody().getUnifiedToken(), RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptAreaCodeKey + nsrsbh, areaCode, RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptGdslxdmKey + nsrsbh, "1", RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptSwjgdmKey + nsrsbh, StringUtils.isNotBlank(swjgdm) ? swjgdm : "000000", RedisKeyConstant.kfptSessionIdLifeTime);
            RedisUtil.set(RedisKeyConstant.redisKfptClientNoKey + nsrsbh, "88888888888", RedisKeyConstant.kfptSessionIdLifeTime);
        }
        return getUnifiedTokenResponse;
    }

    @Override
    public UserCenterBaseResponse switchNsr(SwitchNsrRequest SwitchNsrRequest) {
        UserCenterBaseResponse userCenterBaseResponse = new UserCenterBaseResponse();
        String url = foreseeUsercenterUrl + "/gateway/usercenter/front/org/select?requestId=" + SwitchNsrRequest.getRequestId()
                + "&appId=" + SwitchNsrRequest.getAppId() + "&token=" + SwitchNsrRequest.getToken();
        SwitchNsrBodyDto switchNsrBodyDto = new SwitchNsrBodyDto();
        BeanUtils.copyProperties(SwitchNsrRequest, switchNsrBodyDto);
        ResponseEntity<String> responseEntity = RestTemplateUtil.postForEntityJson(url, JSON.toJSONString(switchNsrBodyDto));
        String responseJson = responseEntity.getBody();
        if (responseJson != null) {
            userCenterBaseResponse = JSON.parseObject(responseJson, UserCenterBaseResponse.class);

        }
        return userCenterBaseResponse;
    }

    @Override
    public UserRegisterResponse register(UserRegisterRequest userRegisterRequest) {
        UserRegisterResponse userRegisterResponse = new UserRegisterResponse();
        String url = foreseeUsercenterUrl + "/v1/usercenter/user/register";
        OpenApiClient openApiClient = new OpenApiClient(foreseeUsercenterAppid, foreseeUsercenterSignKey);
        log.info("======>>>>>register(用户注册)请求参数:{}", JSON.toJSONString(userRegisterRequest));
        String responseJson = openApiClient.requestApp(url, JSON.toJSONString(userRegisterRequest), "10001003");
        log.info("======>>>>>register(用户注册)返回数据:{}", responseJson);
        if (responseJson != null) {
            userRegisterResponse = JSON.parseObject(responseJson, UserRegisterResponse.class);

        }
        return userRegisterResponse;
    }
*/
    @Override
    public Rest<BaseResponse> getCompanyInfoByNsrsbh(KfptDoServiceRequest req) {
      BaseResponse response = new BaseResponse();
        String nsrsbh = "";
        if (StringUtils.isNotBlank(req.getShxydm())) {
            nsrsbh = req.getShxydm();
        } else if (StringUtils.isNotBlank(req.getNsrsbh())) {
            nsrsbh = req.getNsrsbh();
        }
        if (StringUtils.isBlank(nsrsbh)) {
            response.setMsg("请输入纳税人识别号或社会信用代码");
            response.setCode("-1");
            return new Rest(response);
        }
        List<CompanyInfo> companyInfo = companyInfoMapper.getCompanyInfoByNsrsbh(nsrsbh);
        if (companyInfo != null && companyInfo.size() == 0) {
            response.setMsg("根据该纳税人识别号无法查到企业信息");
            response.setCode("-1");
            return new Rest(response);
        }
        response.setCode("0");
        response.setData(companyInfo.get(0));
        return transBaseResponseToRest(response);
    }

    @Override
    public Rest<BaseResponse> loginByNsrsbhOrShxydm(NsrsbhShxydmRequst req) throws Exception {
        Rest<BaseResponse> response = iLoginService.loginByNsrsbhOrShxydm(req);
        return response;
    }

  /*  @Override
    public Rest<BaseResponse> keepSession(String cid) throws Exception {
        BaseResponse response;
        response = swyAPIService.keepSessionByCid(cid);
        return new Rest<>(response);
    }


    @Override
    public Rest<BaseResponse> loginByNsrsbhToSign(NsrsbhToSignRequest req) throws Exception {
        Rest<BaseResponse> response = iLoginService.loginByNsrsbhToSign(req);
        return response;
    }

    @Override
    public Rest<Object> queryUserInfoByNsrsbh(QueryUserInfoByNsrsbhReqDto reqDto) throws Exception {
        if (io.micrometer.core.instrument.util.StringUtils.isBlank(reqDto.getNsrmc()) & io.micrometer.core.instrument.util.StringUtils.isBlank(reqDto.getNsrsbh())) {
            return Rest.error("查询参数不可全为空");
        }
        List<UserInfo> userInfos = userInfoMapperMapper.listByNsrsbhOrNsrmc(reqDto);
        for (UserInfo userinfo : userInfos) {
            if (userinfo.getAreaCode() != null && userinfo.getAreaCode().startsWith("11")) {
                if (io.micrometer.core.instrument.util.StringUtils.isNotBlank(userinfo.getGryhmm())) {
                    userinfo.setGryhmm(Sm4Util.decryptEcb(org.bouncycastle.util.encoders.Hex.toHexString(gryhmmkey.getBytes()), userinfo.getGryhmm()));
                }
            }
            userinfo.setYhmm(RSAUtils.getRsaDecode(userinfo.getYhmm()));
        }
        return new Rest<>(userInfos);
    }

    @Override
    public Rest<BaseResponse> switchQyByNsrsbh(SwitchQyByNsrsbhRequest switchQyByNsrsbhRequest) throws Exception {
        return Rest.ok(iLoginYhzhService.switchQyByNsrsbh(switchQyByNsrsbhRequest));
    }

    public Rest<BaseResponse> transBaseResponseToRest(BaseResponse response) {
        response.setMap(null);
        if (BszsResultEnum.SUCCESS_OLD.getCode().equals(response.getCode())) {
            response.setCode("0");
        }
        return new Rest(response);
    }*/


}